package com.ruoyi.web.cp.service.ob;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.web.cp.service.http.ObHttpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ObGameOnliveService {

    @Autowired
    private ObHttpService httpService;
    public void execute(String key) {
        List<String> mids = getOnliveList();
        if (CollUtil.isEmpty(mids)) {
            log.info("livedata 无数据");
            return;
        }
        for (String mid : mids) {
            getOnliveListDetail(mid);

        }
    }

    public List<String> getOnliveList() {
        List<String> mids = new ArrayList<>();
        JSONArray datas = new JSONArray();
        datas.fluentAdd("livedata");
        JSONObject res = httpService.sendObList(datas);
        if ("0000000".equals(res.getString("code"))
                && CollUtil.isNotEmpty(res.getJSONObject("data"))) {
            JSONObject item = res.getJSONObject("data");
            JSONArray livedatas = item.getJSONArray("livedata");
            for (int i = 0; i < livedatas.size(); i++) {
                String tn = livedatas.getJSONObject(i).getString("tn");
                String mid = livedatas.getJSONObject(i).getString("mids");
                if (tn.contains("VS-PANDA")) {
                    continue;
                }
                mids.add(mid);
            }
        }
        return mids;
    }

    public JSONObject getOnliveListDetail(String mid) {
        JSONObject res = httpService.sendObListDetail(mid, false);
        if ("0000000".equals(res.getString("code"))
                && CollUtil.isNotEmpty(res.getJSONObject("data"))) {
            JSONObject item = res.getJSONObject("data");
            Integer mst = item.getInteger("mst");
            if (mst > 180 && mst <= 36) {

            }
        }
        return res;
    }

}
